package cn.cmcc.online.smsapi.ui.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import cn.cmcc.online.smsapi.ui.manager.IBaseView;
import cn.cmcc.online.smsapi.ui.model.ActionData;
import cn.cmcc.online.smsapi.ui.model.HeadData;
import cn.cmcc.online.smsapi.ui.presenter.GeneralBasePresenter;
import cn.cmcc.online.smsapi.ui.presenter.GeneralBasePresenterImpl;
import cn.cmcc.online.smsapi.ui.util.BackgroundShapeUtils;
import cn.cmcc.online.smsapi.ui.util.DataExchangeProxy;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.util.SimpleAnimationListener;
import hugo.weaving.DebugLog;

import static android.widget.LinearLayout.HORIZONTAL;

/**
 * Header+Body+Footer
 */
public class GeneralBaseVistaImpl implements GeneralBaseVista, IBaseView {
    private static final String TAG = "GeneralBaseVistaImpl";
    public static final boolean WITH_ANIMATION = false;

    private Activity mActivity;
    protected Context mContext = null;
    private GeneralBasePresenter mGeneralBasePresenter = null;
    private View.OnClickListener mSwitchCardListener = null;
    private View.OnClickListener mActionClickListener = null;

    private LinearLayout mViewRoot = null;
    // mViewGroupHead
    private LinearLayout mViewGroupHead;
    private ImageView mImageViewLogo = null;
    private TextView mTextViewTitle = null;
    private TextView mTextViewChange = null;
    // body
    private ViewGroup mViewGroupBody = null;
    private TextView mTextViewContent = null;
    private View mViewCardBody = null;
    // bottom
    private LinearLayout mViewGroupBottom = null;
    private LinearLayout mViewGroupBottomBtns = null;

    public GeneralBaseVistaImpl(Context context) {
        mContext = context;
        init();
        initLayout();
    }

    protected void init() {
        mGeneralBasePresenter = new GeneralBasePresenterImpl();
        mGeneralBasePresenter.setVista(this);

        mSwitchCardListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGeneralBasePresenter.switchCard();
            }
        };
        mActionClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mActivity != null) {
                    notifyDoAction();
                    mGeneralBasePresenter.processAction(mActivity, (ActionData) v.getTag());
                }
            }
        };
    }

    /**
     * 通知当前有服务按钮按下
     */
    protected void notifyDoAction() {

    }

    @DebugLog
    protected void initLayout() {
        mViewRoot = new LinearLayout(mContext);
        mViewRoot.setOrientation(LinearLayout.VERTICAL);
        mViewRoot.setBackgroundColor(Color.TRANSPARENT);
        float[] radii = {DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7)};
        Drawable backgroundDrawable = BackgroundShapeUtils.getBackgroundDrawable(Color.WHITE, 0, 0, radii);
        mViewRoot.setBackground(backgroundDrawable);

        initHead();
        initBody();
        initBottom();
    }

    @DebugLog
    private void initHead() {
        mViewGroupHead = new LinearLayout(mContext);
        mViewGroupHead.setOrientation(HORIZONTAL);
        mViewGroupHead.setGravity(Gravity.CENTER_VERTICAL);

        mImageViewLogo = new ImageView(mContext);
        LinearLayout.LayoutParams logoParams = new LinearLayout.LayoutParams(DensityUtil.dp2px(mContext, 26), DensityUtil.dp2px(mContext, 26));
        logoParams.setMargins(DensityUtil.dp2px(mContext, 15), 0, 0, 0);
        mViewGroupHead.addView(mImageViewLogo, logoParams);

        mTextViewTitle = new TextView(mContext);
        mTextViewTitle.setTextSize(14);
        mTextViewTitle.setTextColor(Color.parseColor("#FFFFFF"));
        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(-2, -2);
        titleParams.setMargins(DensityUtil.dp2px(mContext, 10), 0, 0, 0);
        mViewGroupHead.addView(mTextViewTitle, titleParams);

        mTextViewChange = new TextView(mContext);
        mTextViewChange.setText("原文");
        mTextViewChange.setTextSize(12);
        mTextViewChange.setTextColor(Color.parseColor("#FFFFFF"));
        mTextViewChange.setGravity(Gravity.RIGHT);
        mTextViewChange.setOnClickListener(mSwitchCardListener);
        LinearLayout.LayoutParams originBitParams = new LinearLayout.LayoutParams(-1, -2);
        originBitParams.setMargins(0, 0, DensityUtil.dp2px(mContext, 15), 0);
        mViewGroupHead.addView(mTextViewChange, originBitParams);
        mViewRoot.addView(mViewGroupHead, DensityUtil.dp2px(mContext, 300), DensityUtil.dp2px(mContext, 46));
    }

    @DebugLog
    private void initBody() {
        mViewGroupBody = new FrameLayout(mContext);
        mViewGroupBody.setBackgroundColor(Color.parseColor("#FCFCFC"));
        mTextViewContent = new TextView(mContext);
        mTextViewContent.setTextSize(16);
        mTextViewContent.setVisibility(View.INVISIBLE);
        mTextViewContent.setTextColor(Color.parseColor("#747474"));
        mTextViewContent.setMinHeight(DensityUtil.dp2px(mContext, 97));
        mTextViewContent.setPadding(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 25));
        mViewGroupBody.addView(mTextViewContent);

        mViewRoot.addView(mViewGroupBody, DensityUtil.dp2px(mContext, 300), -2);
    }

    @DebugLog
    private void initBottom() {
        mViewGroupBottom = new LinearLayout(mContext);
        mViewGroupBottom.setOrientation(LinearLayout.VERTICAL);
        View divider = new View(mContext);
        LinearLayout.LayoutParams dividerParam = new LinearLayout.LayoutParams(-1, DensityUtil.dp2px(mContext, 1));
        divider.setBackgroundColor(Color.parseColor("#F4F4F4"));
        mViewGroupBottom.addView(divider, dividerParam);

        mViewGroupBottomBtns = new LinearLayout(mContext);
        mViewGroupBottomBtns.setOrientation(HORIZONTAL);
        mViewGroupBottomBtns.setGravity(Gravity.CENTER_VERTICAL);
        float[] floatsFoot = {0, 0, 0, 0, DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7)};
        Drawable drawableFoot = BackgroundShapeUtils.getBackgroundDrawable(Color.parseColor("#fbfbfb"), 0, 0, floatsFoot);
        mViewGroupBottomBtns.setBackgroundDrawable(drawableFoot);
        mViewGroupBottom.addView(mViewGroupBottomBtns, -1, -1);

        mViewRoot.addView(mViewGroupBottom, DensityUtil.dp2px(mContext, 300), DensityUtil.dp2px(mContext, 50));
    }

    @Override
    public void bind(Context context, JSONObject data) {
        mActivity = (Activity) context;
        mGeneralBasePresenter.bind(data);
    }

    @Override
    public void showHead(HeadData data) {
        BitmapDrawable bitmapDrawable = DataExchangeProxy.getInstance().getBitmap(data.getLogo());
        if (bitmapDrawable != null) {
            mImageViewLogo.setImageBitmap(bitmapDrawable.getBitmap());
        }
        mTextViewTitle.setText(data.getTitle());
        float[] radii = {DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), 0, 0, 0, 0};
        Drawable backgroundDrawable = BackgroundShapeUtils.getBackgroundDrawable(Color.parseColor(data.getColor()), 0, 0, radii);
        mViewGroupHead.setBackground(backgroundDrawable);
    }

    @Override
    public void showBottom(List<ActionData> listAction) {
        mViewGroupBottomBtns.removeAllViews();

        if (listAction != null && listAction.size() > 0) {
            mViewGroupBottom.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams layoutFootButton = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams dividerParam = new LinearLayout.LayoutParams(1, DensityUtil.dp2px(mContext, 34));
            layoutFootButton.weight = 1;
            for (int i = 0; i < listAction.size(); i++) {
                ActionData actionData = listAction.get(i);
                Button button = new Button(mContext);
                button.setBackgroundColor(Color.TRANSPARENT);
                button.setText(actionData.getName());
                button.setTextSize(14);
                button.setTextColor(Color.parseColor("#438a9d"));
                button.setTag(actionData);
                button.setOnClickListener(mActionClickListener);
                mViewGroupBottomBtns.addView(button, layoutFootButton);
                if (i != listAction.size() - 1) {
                    View divider = new View(mContext);
                    divider.setBackgroundColor(Color.parseColor("#d8d8d8"));
                    divider.setLayoutParams(dividerParam);
                    mViewGroupBottomBtns.addView(divider, dividerParam);
                }
            }
        }
    }

    @Override
    public void initDefaultView() {
        mViewGroupBottom.setVisibility(View.GONE);
    }

    @Override
    public void showCard() {
        mTextViewContent.setVisibility(View.GONE);
        mViewCardBody.setVisibility(View.VISIBLE);
        mTextViewChange.setText("原文");
        if (WITH_ANIMATION) {
            int originHeight = mViewGroupBody.getHeight();
            if (originHeight == 0) {
                return;
            }
            mViewGroupBody.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(mViewGroupBody.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int targetHeight = mViewGroupBody.getMeasuredHeight();
            setHeightWithAnimation(originHeight, targetHeight);
    }
    }

    @Override
    public void showContent(String content) {
        mTextViewContent.setText(content);
        mTextViewContent.setVisibility(View.VISIBLE);
        mViewCardBody.setVisibility(View.GONE);
        mTextViewChange.setText("卡片");

        if (WITH_ANIMATION) {
            int originHeight = mViewGroupBody.getHeight();
            if (originHeight == 0) {
                return;
            }
            mViewGroupBody.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.dp2px(mViewGroupBody.getContext(), 300), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int targetHeight = mViewGroupBody.getMeasuredHeight();
            setHeightWithAnimation(originHeight, targetHeight);
        }
    }

    @Override
    public void updateBottom() {
        mGeneralBasePresenter.updateBottom();
    }

    private void setHeightWithAnimation(int originHeight, int targetHeight) {
        ValueAnimator animator = ValueAnimator.ofInt(originHeight, targetHeight).setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mViewGroupBody.getLayoutParams();
                params.height = (int) animation.getAnimatedValue();
                mViewGroupBody.setLayoutParams(params);
            }
        });
        animator.addListener(new SimpleAnimationListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mViewGroupBody.getLayoutParams();
                params.height = -2;
                mViewGroupBody.setLayoutParams(params);
            }
        });
        animator.start();
    }

    @Override
    public View getView() {
        return mViewRoot;
    }

    @Override
    public void dismissDialog() {
    }

    protected <T> T findView(int id) {
        return (T) mViewRoot.findViewById(id);
    }

    protected void addBody(View v) {
        mViewCardBody = v;
        mViewCardBody.setBackgroundColor(Color.TRANSPARENT);
        mViewGroupBody.addView(v);
    }
}
